HW2

107061112 王昊文


Question 1

1.For the Iris dataset, use One-vs.-Rest (OvR) with the following two algorithms to train a model to classify three classes. To obtain the best accuracy of the model, please explain the progress of your work, including how the hyperparameters are tuned.

Implementation

  1. 首先我們利用pandas來讀取資料,並將資料做處理,設定各個column的名稱,賦予他們容易閱讀的名稱來作為他們的index。如:sepal length
  2. 利用課本提供的Code來分別實做Perceptron, Adaline, AdalineSGD。課本預設情況沒有將Feature map在每次epoch更新時隨機一次,而我自己為他加上了隨機函數,因此每次Training的果也會微微不同。
  3. 使用Ovr的方式去implement,我們會將三個獨立分辨Class的Model利用net input的結果去取max來當作我們最後的判斷。
  4. 利用課本的plot_decision_region推廣至可以繪製3個Class。
  5. 將Feature以及Label根據7:3的比例區分為Training set以及Testing Set,Testing Set為均勻的從Feature跟Label中取出,然後並不使用Validation set, 因為資料量實在太小了。
  6. 我們直接使用我們Test set來Cross-Validate最佳learning rate,因為我們沒有使用Validation set來驗證。
  7. 在Iris dataset的例子中,它屬於一個相對簡單的Dataset,只有四個Feature,我們理所當然的可以直接將四個Feature都拿來訓練,但同時我會將Feature兩兩畫出來,看看是否將不重要的Feature取出。

我在showcase每個不同的model時候,我的架構是:

  1. 先將想要的dataset 分類程test and training。
  2. 透過cross validation來尋找最好的Learning rate
  3. 檢視我們的model是否收斂,利用"number of updates"跟"loss curve"來看
  4. 計算準確率來代表這個model的performance
  5. 如果只有2-dimension,將decision boundary繪製出來

Reading data using Pandas

在這個步驟我們將資料讀近來,並印出最後幾行確認資料有被正確讀取

Implement the perceptron method

在此處,我們將課本的Code稍微做了一些小更改,大致脈落是相同的,但每次執行Epoch的時候我們會將Feature稍微做Random洗牌,讓每次Model看到資料的順序都不一樣。

Generate a function to plot the decision region

也是將課本的例子沿用

Extracting the datas for the three individual perceptron model

將X跟y設定好,並透過7:3的比例去區分Training跟Testing Set,此處需要特別注意,我們會分開標準化Training跟Testing Set

Training the perceptron model with four features included

在完全沒有方向的情況下,直接無腦用全部的Feature試試看,利用Random Search來尋找最好的Learning rate。我們會記下達到最好Accuracy的Model。後面我們會試試看如果只用兩個Feature是否會有較好的正確率。

Plotting the "Number of updates" for each class.

我們要確認我們找到的Model有確實收斂

然而我們繪製出來的結果,只有辨別第一類的Model有收斂,顯示可能再切割第二跟第三類的邊界無法完美切割。

Conclude the accuracy of our model

由於我們沒有使用Validation set,我們直接將我們的Test set丟回Model計算正確率。

結論:在我們沒有使用Validation set的情況下,我們得到的Model最終準確率可能會因為我們挑的Test set資料剛好符合某個Learning Rate而出現了Overfitting的情況,但礙於資料數量有限的情況,我們先不要考慮Validation set的情況。以Accuracy來看,我們Train出來的Model應該算不錯。

Now try to pick only two feature and try the accuracy

In order to acheive better visualization, we want to find the most dominant two features that affects the final result Since We discover that only the classifier for Setosa will converge, we want to test out what are the two features that will achieve the best testing accuracy

使用Seaborn來看各個Feature之間的狀況

從上圖中我們可以發現,"Petal Length" - "Petal Width"應該是最難用線性模型切割的,因為點跟點之間都重疊了。然後,"Petal Length" - "Petal Width"看起來是最好切的,因此我們使用這兩個Feature來做測試。一樣使用隨機來尋找最好的Learning rate來達到最高的準確率。

Plot the "number of updates" curve

檢視如果只有挑選兩個Feature所訓練出來的是否收斂。

我們一樣發現Model 仍然不會收斂

以上方Train出來的Decision Boundary來說,其實表現是相當不錯的。少許幾個綠點跑進了藍色區域,但由圖中看到他的資料點是有少許糾結的。由上面可以看到,雖然從四個Feature Train出來的準確率比較高,但因為Dataset比較小,我們又沒有使用Validation set的情況下,我們挑選到的Testset可能在選擇四個Feature的情況下比較Match原本的分佈。

在這個Training set比較小的情況下我們只能妥協使用這樣的方法,當Dataset變大時可以使用Validation set去挑選最好的Hyperparameter,這樣可以避免因為Testing data overfit的問題導致準確率飆高。

最後,撇除Overfitting的問題,其實四個Feature跟兩個Feature訓練出來的準確率其實差不多,其實我們可以純粹抽取sepal length跟sepal width來當作我們訓練的Feature,這樣可以加速Training time跟test time。

2. Implementing an adaptive linear(AdaLine) neuron in Python

第二題使用Adaline來實做,流程與上一題相同。我們一樣使用課本的code,並在每個Epoch亂數我們Feature矩陣。

Training Adaline using four features of the Iris dataset

PLot the loss function and the accuracy for our best model

在Feature全部使用的情況,Perceptron得到了比較高的準確率,這取決於了兩個演算法所Optimize的方向不同,一個使用既定的Update rule,另外一個是針對Loss去Optimize,因此我們切割出來的Decision Boundary自然就不同,進而導致了Accuracy的差距。

Repeat and choose two of the best features to implement Adaline like we did in Perceptron

Same here, to reduce the complexity and better visualization, we use the two most linearly seperable features to implement Adaline

Plot the loss funtion of our two feature Adaline model

The final decision boundary for our 2 feature Adaline model

我們可以看到了,如果我們一樣使用兩個Feature來分別訓練Perceptron跟Adaline,Perceptron仍然表現的比較好。我個人認為Adaline以及Perceptron之間並沒有優劣,準確率可能因為Update rule訓練出來的Boundary不同,在加上可能我們Test set剛好切在錯誤的區域導致的錯誤率偏高。

3. Implementing Adaline SGD in Python

第三題使用AdalineSGD來實做,我們一樣使用課本的code,並在每個Epoch亂數我們Feature矩陣。

First we use all the features and cross-validate to find the greatest model

Plot the loss curve for our best model

Repeat and choose two of the best features to implement AdalineSGD like we did in Perceptron and Adaline

Same here, to reduce the complexity and better visualization, we use the two most linearly seperable features to implement Adaline

The final decision boundary for our 2 feature AdalineSGD model

由以上結果來看,Adaline以及AdalineSGD表現是差不多的,也驗證了兩者應該趨於差不多的理論。

Question 1 Summary: 在Iris dataset中,perceptron表現較優。但Iris dataset屬於線性難分割的Data,只能說在這個特定的情況下Perceptron較為突出。

然後我們也看到了Perceptron可以直接使用兩個Feature來Train,得到與四個Feature差不多的準確率,這樣就可以提昇Training的效率。然而Adaline似乎還是使用四個Feature來Train得到的準確率比較高。在這個Case中因為我們資料量並不大,AdalineSGD的優勢也沒有明顯發揮。

Question 2

2.For the wine dataset (https://archive.ics.uci.edu/ml/datasets/Wine), please train a model with the algorithms you have learned now to classify three kinds of wine.

Implementation

  1. 首先我們利用pandas來讀取資料,並將資料做處理,設定各個column的名稱。
  2. 在這裡我們使用上題提到的三種Algorithm來試試看整體準確率,然後因為是Multi-class,我們使用Ovr的方法
  3. 這裡因為資料量比較大,共178個,我們嘗試看看將整個Training set分成Test set(共28個)來當作最後評估模型準確率的,然後剩下的依據7:3的比例分為Training set以及Validation set避免Overfitting的問題。
  4. 使用Test set來評估我們模型的準確率
  5. 因為Feature比較多,是個比較複雜的模型,我們直接用所有的Feature來訓練。

Reading the wine dataset

Extract the data

在這裡我們將資料分成Train, validation, test

Using the perceptron model to train and cross-validation

使用Train set來訓練然後用Validation set來Cross validate

Plot the number of update for our best model

檢視模型是否收斂

這次的Perceptron有正常收斂!

Use the test set to verify our accuracy

Now trying Adaline using the same sets

重複上面,我們改用Adaline試試看

Extract data

Train and Cross-Validate

Plot the loss curve of the best model determined using cross-validation

Use the test set to verify our accuracy

在這個Case之下與Perceptron有差不多的表現,應該是資料點比較Linearly Seperable的原因,讓兩種演算法都能夠成功完美切割。

Now trying AdalineSGD using the same sets

Plot the loss curve of the best model determined using cross-validation

Use the test set to verify our accuracy

預期之下,AdalineSGD跟Adaline應該要有差不多的準確率,而這裡驗證了我們的預期。

Question 2 Summary: 比起在Iris的case中我們無法直接視覺化,但在Wine的例子中三種不同模型得到的準確率都高於Iris dataset,原因應該就是因為Data比較好分割,雖然我們無法視覺化。因此在Wine的案例中,三種演算法的好壞應該是差不多的。

Discussion

我的專題是直接從Deep Learning開始做,其實有很多ML的基礎感覺不是很扎實。這次的作業透過手刻最簡單的Linear Classfier來讓我真正看見Classifier內部的運作方式,還有關於資料的處理等等我是第一次手刻,雖然比起直接Call Funtion困難許多,但我覺得透過這樣的方式可以真正檢視自己有沒有學會。像是標準化,我們應該要很清楚自己的Test data不能跟Training data一起丟進去標準化,剛開始的我直接混在一起了,還有關於Cross validation,Gradinet update等等,經過手刻之後更讓我清楚了內部的運作方式,流程都要很清楚了。把它思考成Computation graph才能寫出Code.

另外我原本以為越複雜的Model表現會越好,比如以這個作業來說,Adaline跟Perceptron,我原本以為Adaline的表現會比較好,但在Iris Dataset之下結果反而是Perceptron,這也說明了ML領域沒有一個直接的數學公式可以直接說哪種Model比較好,只能透過各種不同嘗試去尋找最好的解法。

另外再做Wine Dataset的時候,因為Feature比較多很難視覺化,讓我很期待後面的課程,究竟如何降低一筆高維度的資料到低維度,我很好奇他轉換的過程。希望可以學到這個方法,因為在Training的過程中明顯感覺到它比Iris dataset Training還要久,雖然我們最終得到了很好的準確率,但設計一個好的模型除了準確率,效能也是我們要考慮的。如果手邊運算資源不足的話可能會讓使用者端的使用體驗變差。